package cn.stylefeng.guns.modular.fruits.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.fruits.entity.FruitsBasicCustomer;
import cn.stylefeng.guns.modular.fruits.mapper.FruitsBasicCustomerMapper;
import cn.stylefeng.guns.modular.fruits.model.params.FruitsBasicCustomerParam;
import cn.stylefeng.guns.modular.fruits.model.result.FruitsBasicCustomerResult;
import  cn.stylefeng.guns.modular.fruits.service.FruitsBasicCustomerService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * 客户管理 服务实现类
 */
@Service
public class FruitsBasicCustomerServiceImpl extends ServiceImpl<FruitsBasicCustomerMapper, FruitsBasicCustomer> implements FruitsBasicCustomerService {

    @Override
    public void add(FruitsBasicCustomerParam param){
        FruitsBasicCustomer entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(FruitsBasicCustomerParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(FruitsBasicCustomerParam param){
        FruitsBasicCustomer oldEntity = getOldEntity(param);
        FruitsBasicCustomer newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public FruitsBasicCustomerResult findBySpec(FruitsBasicCustomerParam param,Long operateId){
        return this.baseMapper.findBySpec(param, operateId);
    }

    @Override
    public List<FruitsBasicCustomerResult> findListBySpec(FruitsBasicCustomerParam param,Long operateId){
        return this.baseMapper.findListBySpec(param, operateId);
    }

    @Override
    public LayuiPageInfo findPageBySpec(FruitsBasicCustomerParam param,String condition,Long operateId){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition, operateId);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(FruitsBasicCustomerParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private FruitsBasicCustomer getOldEntity(FruitsBasicCustomerParam param) {
        return this.getById(getKey(param));
    }

    private FruitsBasicCustomer getEntity(FruitsBasicCustomerParam param) {
        FruitsBasicCustomer entity = new FruitsBasicCustomer();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
